package com.sixj.keycloak;

import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.storage.StorageId;
import org.keycloak.storage.adapter.AbstractUserAdapterFederatedStorage;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author sixiaojie
 * @date 2025-07-10-16:20
 */
public class EmployeeUserAdapter extends AbstractUserAdapterFederatedStorage {
    protected Employee entity;
    protected String keycloakId;

    public EmployeeUserAdapter(KeycloakSession session, RealmModel realm, ComponentModel model, Employee entity) {
        super(session, realm, model);
        this.entity = entity;
        keycloakId = StorageId.keycloakId(model, entity.getId());
    }

    public String getPassword() {
        return entity.getPassword();
    }

    public void setPassword(String password) {
        entity.setPassword(password);
    }

    @Override
    public String getUsername() {
        return entity.getEmpNo();
    }

    @Override
    public void setUsername(String username) {
        entity.setEmpName(username);
    }

    @Override
    public void setEmail(String email) {
        entity.setEmail(email);
    }

    @Override
    public String getEmail() {
        return entity.getEmail();
    }

    @Override
    public String getId() {
        return keycloakId;
    }

    @Override
    public String getFirstName() {
        return entity.getEmpName();
    }

    @Override
    public void setFirstName(String firstName) {
        // do nothing
    }

    @Override
    public String getLastName() {
        return "";
    }

    @Override
    public void setLastName(String lastName) {
        // do nothing
    }

    @Override
    public void setSingleAttribute(String name, String value) {
        if (name.equals("phone")) {
            entity.setMobile(value);
        } else if (name.equals("email")) {
            entity.setEmail(value);
        } else {
            super.setSingleAttribute(name, value);
        }
    }

    @Override
    public void removeAttribute(String name) {
        if (name.equals("phone")) {
            entity.setMobile(null);
        } else if (name.equals("email")) {
            entity.setEmail(null);
        } else {
            super.removeAttribute(name);
        }
    }

    @Override
    public void setAttribute(String name, List<String> values) {
        if (name.equals("phone")) {
            entity.setMobile(values.get(0));
        } else if (name.equals("email")) {
            entity.setEmail(values.get(0));
        } else {
            super.setAttribute(name, values);
        }
    }

    @Override
    public String getFirstAttribute(String name) {
        if (name.equals("phone")) {
            return entity.getMobile();
        }else if (name.equals("email")) {
            return entity.getEmail();
        } else {
            return super.getFirstAttribute(name);
        }
    }

    @Override
    public Map<String, List<String>> getAttributes() {
        Map<String, List<String>> attrs = super.getAttributes();
        MultivaluedHashMap<String, String> all = new MultivaluedHashMap<>();
        all.putAll(attrs);
        all.add("phone", entity.getMobile());
        all.add("name", entity.getEmpName());
        all.add("email", entity.getEmail());

        return all;
    }

    @Override
    public List<String> getAttribute(String name) {

        if (name.equals("phone")) {
            List<String> phone = new LinkedList<>();
            phone.add(entity.getMobile());
            return phone;
        } else if (name.equals("email")) {
            List<String> email = new LinkedList<>();
            email.add(entity.getEmail());
            return email;
        } else {
            return super.getAttribute(name);
        }
    }
}
